<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title></title> <#include "head.ftl" />
</head>
<body>
	<div class="body-box">
		<div class="rhead">
			<div class="rpos">当前位置: 系统通知 - 添加</div>
			<form class="ropt">
				<input type="button" value="返回" onclick="location.href='${st.domain}/notice/list'" />
			</form>
			<div class="clear"></div>
		</div>
		<form id="notice_form">
			<table width="100%" class="pn-ftable" cellpadding="2" cellspacing="1" border="0">
				<tr>
					<td width="10%" class="pn-flabel pn-flabel-h"><span class="pn-frequired">*</span>用户:</td>
					<td colspan="1" class="pn-fcontent"><input type="text" class="required" size="25"
						id="member" /> &nbsp; <span style="color:#999">用户ID,多个请用,隔开，不填则发送给所有用户</span>
					</td>
					<input type="hidden" id="notice_type" name="type" value="${notice.type}" />
				</tr>
				<tr>
					<td width="10%" class="pn-flabel pn-flabel-h"><span class="pn-frequired">*</span>标题:</td>
					<td colspan="1" class="pn-fcontent"><input type="text" name="title"
						value="${notice.title}" class="required" size="25" maxlength="150" />
					</td>
				</tr>
				<tr>
					<td class="pn-flabel pn-flabel-h"><span class="pn-frequired">*</span>正文:</td>
					<td colspan="1" class="pn-fcontent"><textarea cols="40" rows="6" name="content"
							maxlength="255">${notice.content}</textarea></td>
				</tr>
				<tr>
					<td class="pn-flabel pn-flabel-h">摘要:</td>
					<td colspan="1" class="pn-fcontent"><textarea cols="40" rows="3" name="summary"
							maxlength="255">${notice.summary}</textarea></td>
				</tr>
				<tr>
					<td colspan="4" class="pn-fbutton"><input type="hidden" name="noticeId"
						value="${notice.noticeId}" /><input type="button" value="提交" id="noticeBt" /> &nbsp; <input
						type="reset" id="bt_reset" value="重置" />
					</td>
				</tr>
			</table>
		</form>
		<div id="addingMsg" style="font-size:15px;color:blue;"></div>
	</div>
</body>
<script>
	//编辑通知请求路径
	var addDoPath = g_domain + "/notice/addDo/json";
	//用户绑定通知请求路径
	var addToMemberPath = g_domain + "/memberNotice/addDo/json";
	// 是否正在执行操作
	var isAdding = false;
	var $noticeForm = $("#notice_form");
	
	
	//事件：提交通知
	$("#noticeBt").click(function(){
		// 避免重复操作
		if (isAdding) {
			return;
		}
		isAdding = true;
		var summary = $("[name='summary']").val();
		if("" == summary){
			//“摘要”为空时，从“正文”中截取部分内容给其填值
			summary = $("[name='content']").val().split("，", 2);
			$("[name='summary']").val(summary);
		}
		var memberIds = $("#member").val();
		if("" != memberIds && memberIds.indexOf(",") < 0){
			//用户项不为空且不包含‘,’，则为指定用户
			$("#notice_type").val(0);
		} else {
			//多用户
			$("#notice_type").val(1);
		}
		$.post(addDoPath, $noticeForm.serialize(), function(data){
			if(data.status === 0){
				$noticeForm.hide();
				var noticeId = data.result.noticeId;
				addToMember(noticeId, memberIds, 1);
			} else {
				alert(data.message);
			}
		}, "json");
	});
	
	function addToMember(noticeId, memberIds, nowPage){
		$.post(addToMemberPath, {noticeId:noticeId, memberIds:memberIds, nowPage:nowPage}, function(data){
			if(data.status !== 0){
				alert(data.message);
				return;
			}
			// 没有用户Id,说明是分批发送
			if (!memberIds) {
				var pageInfo = data.result;
				if (pageInfo.nowPage < pageInfo.totalPage) {
					$("#addingMsg").html("目标用户数:" + pageInfo.totalResult + ",已发送用户数:" + (pageInfo.nowPage * pageInfo.pageSize) + 
				",操作还在处理中....请等待!");
					addToMember(noticeId, memberIds, ++pageInfo.nowPage);
				} else {
					isAdding = false;
					$noticeForm.show();
					$("#addingMsg").html("操作完成!共计用户数:" + pageInfo.totalResult);
					alert(data.message);
					$("#bt_reset").trigger("click");
				}
			} else {
				isAdding = false;
				$noticeForm.show();
				// 字符串转数组
				var memberIdsArray = memberIds.split(",");
				$("#addingMsg").html("操作完成!共计用户数:" + memberIdsArray.length);
				alert(data.message);
				$("#bt_reset").trigger("click");
			}
			
		}, "json");
	}

</script>
</html>